<template>
    <div>
        <div class="e-table" style="background-color: #fff" v-if="showTable">
            <!-- 头部buttons -->
            <div class="top">
                <div class="left">
                    <div v-if="isAdd">
                        <el-button size="small" class="btn-greenYellow" @click="importList"
                        >引入清单</el-button>
                    </div>
                    <div class="margin-left-20" v-if="isShowRadio && !isAdd && !isEdit && contractType !== 2">
                        <el-radio-group v-model="radio">
                            <el-radio :label="1">细目名称——>结构名称</el-radio>
                            <el-radio :label="2">结构名称——>细目名称</el-radio>
                        </el-radio-group>
                    </div>
                </div>
                 <div class="right" v-if="radio === 1">
                    <div>
                        <el-input
                            placeholder="请输入细目编号、细目名称或结构部位进行搜索"
                            size="small"
                            v-model="searchKey"
                            @blur="search"
                        >
                            <i slot="suffix" class="el-input__icon el-icon-search" @click="search"></i>
                        </el-input>
                    </div>
                </div>
            </div>
            <div class="errorMsg" v-if="errorMsg.length>0">
                <span >
                    {{ errorMsg.join(',') }}
                </span>
            </div>

            <div v-if="radio === 1">
                <!-- 招标清单 -->
                <div class="e-tree-table" v-if="isBidding === 0 && contractType !== 2">
                    <el-table
                            v-show="!isSearch"
                            ref="tableRef"
                            border
                            style="width: 100%"
                            row-key="billId"
                            :data="tableData"
                            :tree-props="{
                                children: 'children',
                                hasChildren: 'hasChildren'
                            }"
                            :load="onload"
                            lazy
                            :row-class-name="tableRowClassName"
                            class="table"
                            :max-height="$store.state.tableHeight"
                            highlight-current-row
                            v-sortTable="{ 'tableData': tableData, '_this': this, 'ref': 'tableRef' }"
                        >
                            <el-table-column label="操作" width="200" align="left">
                                <template slot-scope="{ row }">
                                    <div class="oparation_box">
                                        <i class="el-icon-delete"
                                            style="font-size: 24px; color: #e9513e;"
                                            @click="onDelNode(row)"
                                            v-if="isAdd"
                                            title="删除"
                                        ></i>
                                    </div>
                                </template>
                            </el-table-column>
                            <el-table-column prop="itemNo" label="细目编号" min-width="200" align="left">
                                <template slot-scope="{ row }">
                                    <span>{{ row.itemNo }}</span>
                                </template>
                            </el-table-column>
                            <el-table-column prop="itemName" label="细目名称" min-width="200" align="left">
                                <template slot-scope="{ row }">
                                    <span>{{ row.itemName }}</span>
                                </template>
                            </el-table-column>
                            <el-table-column prop="structureNamePaths" label="结构部位" width="300">
                                <template slot-scope="{ row }">
                                    <span>{{ row.structureNamePaths }}</span>
                                </template>
                            </el-table-column>
                            <el-table-column prop="unit" label="计量单位" width="120">
                                <template slot-scope="{ row }">
                                    <span>{{ row.unit }}</span>
                                </template>
                            </el-table-column>
                            <el-table-column prop="identification" label="标识" width="120">
                                <template slot-scope="{ row }">
                                    <span>{{ row.identification }}</span>
                                </template>
                            </el-table-column>
                            <el-table-column prop="unitPriceType" label="单价类型" width="120">
                                <template slot-scope="{ row }">
                                    <el-select
                                        v-model="row.unitPriceType"
                                        placeholder="请选择"
                                            v-if="row.listIsDetailed && isAdd && !row.structureId"
                                            @change="unitPriceTypeChange(row)"
                                    >
                                        <el-option
                                            label="全单价"
                                            :value="1"
                                        ></el-option>
                                        <el-option
                                            label="劳务单价"
                                            :value="2"
                                        ></el-option>
                                    </el-select>
                                        <div v-else>{{ ['全单价','劳务单价'][row.unitPriceType-1] }}</div>
                                </template>
                            </el-table-column>
                            <el-table-column prop="quantity" label="数量" class-name="historyData" width="200">
                                <template slot-scope="{ row }">
                                    <span v-if="row.unit === '总额'">--</span>
                                    <span v-else-if="!row.itemIsDetailed && !row.structureId"></span>
                                    <span v-else>{{ row.quantity }}</span>
                                </template>
                            </el-table-column>
                            <el-table-column prop="unitPrice"  label="单价" width="200">
                                <template slot-scope="{ row }">
                                    <el-input
                                        v-model="row.unitPrice"
                                        placeholder="请输入内容"
                                        v-if="row.unit !== '总额' && row.itemIsDetailed && !row.structureId && isEdit"
                                        v-enterFloat
                                        @change="unitPriceChange(row)"
                                    ></el-input>
                                    <span v-else-if="row.unit === '总额'">--</span>
                                    <span v-else-if="!row.itemIsDetailed && !row.structureId"></span>
                                    <span v-else>{{ row.unitPrice }}</span>
                                    <historyUpdateTable
                                        v-if="checkEmpty(row.priceRecords)"
                                        @clickIcon="clickIcon(row.priceRecords)"
                                        :historyTable="historyTableData"
                                    />
                                </template>
                            </el-table-column>
                            <el-table-column prop="amount" label="金额(元)" class-name="historyData" width="120">
                                <template slot-scope="{ row }">
                                    <el-input
                                        v-model="row.amount"
                                        placeholder="请输入内容"
                                        v-if="row.structureId && showParentUnit(row) ==='总额' && isEdit"
                                        @change="changeAmount(row)"
                                        v-enterAllInteger1
                                    ></el-input>
                                    <span v-else>{{ row.amount }}</span>
                                    <historyUpdateTable
                                    v-if="checkEmpty(row.amountRecords)"
                                        @clickIcon="clickIcon(row.amountRecords)"
                                        :historyTable="historyTableData"
                                    />
                                </template>
                            </el-table-column>
                    </el-table>

                    <el-table v-if="isSearch" ref="tableRef1"
                        border
                        style="width: 100%"
                        row-key="billId"
                        :data="searchData"
                        :tree-props="{
                            children: 'children',
                            hasChildren: 'hasChildren'
                        }"
                        :load="onload"
                        lazy
                        :row-class-name="tableRowClassName"
                        class="table"
                        :max-height="$store.state.tableHeight"
                        highlight-current-row
                        v-sortTable="{ 'tableData': searchData, '_this': this, 'ref': 'tableRef1' }"
                    >
                        <el-table-column  label="操作" width="200" align="left">
                        </el-table-column>
                        <el-table-column prop="itemNo" label="细目编号" min-width="200" align="left">
                                <template slot-scope="{ row }">
                                    <span>{{ row.itemNo }}</span>
                                </template>
                        </el-table-column>
                        <el-table-column prop="itemName" label="细目名称" min-width="200" align="left">
                            <template slot-scope="{ row }">
                                <span>{{ row.itemName }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="structureNamePaths" label="结构部位" width="30">
                            <template slot-scope="{ row }">
                                <span>{{ row.structureNamePaths }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="unit" label="计量单位" width="120">
                            <template slot-scope="{ row }">
                                <span>{{ row.unit }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="identification" label="标识" width="120">
                            <template slot-scope="{ row }">
                                <span>{{ row.identification }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="unitPriceType" label="单价类型" width="120">
                            <template slot-scope="{ row }">
                                <el-select
                                    v-model="row.unitPriceType"
                                    placeholder="请选择"
                                        v-if="row.listIsDetailed && isAdd"
                                        @change="unitPriceTypeChange(row)"
                                >
                                    <el-option
                                        label="全单价"
                                        :value="1"
                                    ></el-option>
                                    <el-option
                                        label="劳务单价"
                                        :value="2"
                                    ></el-option>
                                </el-select>
                                    <div v-else>{{ ['全单价','劳务单价'][row.unitPriceType-1] }}</div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="quantity" label="数量" class-name="historyData" width="200">
                            <template slot-scope="{ row }">
                                <span v-if="row.unit === '总额'">--</span>
                                <span v-else-if="!row.itemIsDetailed && !row.structureId"></span>
                                <span v-else>{{ row.quantity }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="unitPrice" label="单价" class-name="historyData" width="200">
                            <template slot-scope="{ row }">
                                <el-input
                                    v-model="row.unitPrice"
                                    placeholder="请输入内容"
                                    v-if="row.unit !== '总额' && row.itemIsDetailed && !row.structureId && isEdit"
                                    v-enterFloat
                                    @change="unitPriceChange(row)"
                                ></el-input>
                                <span v-else-if="row.unit === '总额'">--</span>
                                <span v-else-if="!row.itemIsDetailed && !row.structureId"></span>
                                <span v-else>{{ row.unitPrice }}</span>
                                <historyUpdateTable
                                    v-if="checkEmpty(row.priceRecords)"
                                    @clickIcon="clickIcon(row.priceRecords)"
                                    :historyTable="historyTableData"
                                />
                            </template>
                        </el-table-column>
                        <el-table-column prop="amount" label="金额(元)" class-name="historyData" width="120">
                            <template slot-scope="{ row }">
                                <el-input
                                    v-model="row.amount"
                                    placeholder="请输入内容"
                                    v-if="row.structureId && showParentUnit(row) ==='总额' && isEdit"
                                    @change="changeAmount(row)"
                                    v-enterAllInteger1
                                ></el-input>
                                <span v-else>{{ row.amount }}</span>
                                <historyUpdateTable
                                v-if="checkEmpty(row.amountRecords)"
                                    @clickIcon="clickIcon(row.amountRecords)"
                                    :historyTable="historyTableData"
                                />
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
                <!-- 目标预算清单 -->
                <div class="e-tree-table" v-else-if="isBidding === 1">
                    <el-table v-show="!isSearch" ref="tableRef"
                        border
                        style="width: 100%"
                        row-key="billId"
                        :data="tableData"
                        :tree-props="{
                            children: 'children',
                            hasChildren: 'hasChildren'
                        }"
                        :load="onload"
                        lazy
                        :row-class-name="tableRowClassName"
                        class="table"
                        :max-height="$store.state.tableHeight"
                        highlight-current-row
                        v-sortTable="{ 'tableData': tableData, '_this': this, 'ref': 'tableRef' }"
                    >
                        <el-table-column label="操作" width="200" align="left">
                            <template slot-scope="{ row }">
                                <div class="oparation_box">
                                    <!-- <img src="@/assets/btn/sub.png" class="add_sub"
                                        @click="addChilden(row)"
                                        v-if="(row.listIsDetailed || row.identification === '项') && !row.structureId && isAdd"
                                        title="添加子级"
                                        /> -->
                                    <i class="el-icon-delete"
                                        style="font-size: 24px; color: #e9513e;"
                                        v-if="isAdd"
                                        @click="onDelNode(row)"
                                        title="删除"
                                    ></i>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="itemNo" label="细目编号" min-width="200" align="left">
                            <template slot-scope="{ row }">
                                <el-input
                                    v-model="row.itemNo"
                                    placeholder="请输入内容"
                                    v-if="row.identification === '项' && !row.structureId && isAdd"
                                ></el-input>
                                <div v-else>{{ row.itemNo }}</div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="itemName" label="细目名称" min-width="200" align="left">
                            <template slot-scope="{ row }">
                                <el-input
                                    v-model="row.itemName"
                                    placeholder="请输入内容"
                                    v-if="row.identification === '项' && !row.structureId && isAdd"
                                ></el-input>
                                <div v-else>{{ row.itemName }}</div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="structureNamePaths" label="结构部位" width="300">
                            <template slot-scope="{ row }">
                                <div>{{ row.structureNamePaths }}</div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="unit" align="center" label="计量单位" width="120">
                            <template slot-scope="{ row }">
                                <el-select
                                    v-model="row.unit"
                                    v-if="row.itemIsDetailed && row.identification === '项' && isAdd"
                                    @change="changeUnit(row)"
                                >
                                    <el-option
                                        v-for="item in unitList"
                                        :value="item.label"
                                        :label="item.label"
                                        :key="item.label"
                                    >
                                    </el-option>
                                </el-select>
                                <div v-else>{{ row.unit }}</div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="identification"  align="center"  label="标识" width="120">
                            <template slot-scope="{ row }">
                                <div v-if="!row.structureId">{{ row.identification }}</div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="unitPriceType"  align="center"  label="单价类型" width="150">
                            <template slot-scope="{ row }">
                                <el-select
                                    v-model="row.unitPriceType"
                                    placeholder="请选择"
                                        v-if="row.listIsDetailed && isAdd"
                                        @change="unitPriceTypeChange(row)"
                                >
                                    <el-option
                                        label="全单价"
                                        :value="1"
                                    ></el-option>
                                    <el-option
                                        label="劳务单价"
                                        :value="2"
                                    ></el-option>
                                </el-select>
                                    <div v-else>{{ ['全单价','劳务单价'][row.unitPriceType-1] }}</div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="quantity" label="数量" class-name="historyData"  align="right"  width="120">
                            <template slot-scope="{ row }">
                                <el-input
                                    v-model="row.quantity"
                                    placeholder="请输入内容"
                                    v-if="row.structureId && showParentUnit(row) !=='总额' && isEdit"
                                    v-enterFloat2
                                    @change="quantityChange(row)"
                                ></el-input>
                                <span v-else-if="row.unit === '总额'">--</span>
                                <span v-else-if="!row.itemIsDetailed && !row.structureId"></span>
                                <span v-else>{{ row.quantity }}</span>
                                <historyUpdateTable
                                    v-if="checkEmpty(row.numberRecords)"
                                    @clickIcon="clickIcon(row.numberRecords)"
                                    :historyTable="historyTableData"
                                />
                            </template>
                        </el-table-column>
                        <el-table-column prop="unitPrice" label="单价" class-name="historyData" align="right" width="120">
                            <template slot-scope="{ row }">
                                <el-input
                                    v-model="row.unitPrice"
                                    placeholder="请输入内容"
                                    v-if="showParentUnit(row)  !== '总额' && row.itemIsDetailed && !row.structureId && isEdit"
                                    v-enterFloat
                                    @change="unitPriceChange(row)"
                                ></el-input>
                                <div v-else-if="row.unit === '总额'">--</div>
                                <span v-else-if="!row.itemIsDetailed && !row.structureId"></span>
                                <div v-else>{{ row.unitPrice }}</div>
                                <historyUpdateTable
                                    v-if="checkEmpty(row.priceRecords)"
                                    @clickIcon="clickIcon(row.priceRecords)"
                                    :historyTable="historyTableData"
                                />
                            </template>
                        </el-table-column>
                        <el-table-column prop="amount" label="金额(元)" class-name="historyData" align="right" width="120">
                            <template slot-scope="{ row }">
                                <el-input
                                    v-model="row.amount"
                                    placeholder="请输入内容"
                                    v-if="row.structureId && showParentUnit(row) ==='总额' && isEdit"
                                    @change="changeAmount(row)"
                                    v-enterAllInteger1
                                ></el-input>
                                <div v-else>{{ row.amount }}</div>
                                <historyUpdateTable
                                    v-if="checkEmpty(row.amountRecords)"
                                    @clickIcon="clickIcon(row.amountRecords)"
                                    :historyTable="historyTableData"
                                />
                            </template>
                        </el-table-column>
                    </el-table>

                    <el-table v-if="isSearch" ref="tableRef1"
                        border
                        style="width: 100%"
                        row-key="billId"
                        :data="searchData"
                        :tree-props="{
                            children: 'children',
                            hasChildren: 'hasChildren'
                        }"
                        :load="onload"
                        lazy
                        :row-class-name="tableRowClassName"
                        class="table"
                        :max-height="$store.state.tableHeight"
                        highlight-current-row
                        v-sortTable="{ 'tableData': searchData, '_this': this, 'ref': 'tableRef1' }"
                    >
                        <el-table-column  label="操作" width="200" align="left">
                        </el-table-column>
                        <el-table-column prop="itemNo" label="细目编号" min-width="200" align="left">
                            <template slot-scope="{ row }">
                                <el-input
                                    v-model="row.itemNo"
                                    placeholder="请输入内容"
                                    v-if="row.identification === '项' && !row.structureId && isAdd"
                                ></el-input>
                                <div v-else>{{ row.itemNo }}</div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="itemName" label="细目名称" min-width="200" align="left">
                            <template slot-scope="{ row }">
                                <el-input
                                    v-model="row.itemName"
                                    placeholder="请输入内容"
                                    v-if="row.identification === '项' && !row.structureId && isAdd"
                                ></el-input>
                                <div v-else>{{ row.itemName }}</div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="structureNamePaths" label="结构部位" width="300">
                            <template slot-scope="{ row }">
                                <div>{{ row.structureNamePaths }}</div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="unit" align="center" label="计量单位" width="120">
                            <template slot-scope="{ row }">
                                <el-select
                                    v-model="row.unit"
                                    v-if="row.itemIsDetailed && row.identification === '项' && isAdd"
                                    @change="changeUnit(row)"
                                >
                                    <el-option
                                        v-for="item in unitList"
                                        :value="item.label"
                                        :label="item.label"
                                        :key="item.label"
                                    >
                                    </el-option>
                                </el-select>
                                <div v-else>{{ row.unit }}</div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="identification"  align="center"  label="标识" width="120">
                            <template slot-scope="{ row }">
                                <div v-if="!row.structureId">{{ row.identification }}</div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="unitPriceType"  align="center"  label="单价类型" width="150">
                            <template slot-scope="{ row }">
                                <el-select
                                    v-model="row.unitPriceType"
                                    placeholder="请选择"
                                        v-if="row.listIsDetailed && isAdd && !row.structureId"
                                        @change="unitPriceTypeChange(row)"
                                >
                                    <el-option
                                        label="全单价"
                                        :value="1"
                                    ></el-option>
                                    <el-option
                                        label="劳务单价"
                                        :value="2"
                                    ></el-option>
                                </el-select>
                                    <div v-else>{{ ['全单价','劳务单价'][row.unitPriceType-1] }}</div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="quantity" label="数量" class-name="historyData"  align="right"  width="120">
                            <template slot-scope="{ row }">
                                <el-input
                                    v-model="row.quantity"
                                    placeholder="请输入内容"
                                    v-if="row.structureId && showParentUnit(row) !=='总额' && isEdit"
                                    v-enterFloat2
                                    @change="quantityChange(row)"
                                ></el-input>
                                <span v-else-if="row.unit === '总额'">--</span>
                                <span v-else-if="!row.itemIsDetailed && !row.structureId"></span>
                                <span v-else>{{ row.quantity }}</span>
                                <historyUpdateTable
                                    v-if="checkEmpty(row.numberRecords)"
                                    @clickIcon="clickIcon(row.numberRecords)"
                                    :historyTable="historyTableData"
                                />
                            </template>
                        </el-table-column>
                        <el-table-column prop="unitPrice" label="单价" class-name="historyData" align="right" width="120">
                            <template slot-scope="{ row }">
                                <el-input
                                    v-model="row.unitPrice"
                                    placeholder="请输入内容"
                                    v-if="row.unit !== '总额' && row.itemIsDetailed && !row.structureId && isEdit"
                                    v-enterFloat
                                    @change="unitPriceChange(row)"
                                ></el-input>
                                <div v-else-if="row.unit === '总额'">--</div>
                                <span v-else-if="!row.itemIsDetailed && !row.structureId"></span>
                                <div v-else>{{ row.unitPrice }}</div>
                                <historyUpdateTable
                                    v-if="checkEmpty(row.priceRecords)"
                                    @clickIcon="clickIcon(row.priceRecords)"
                                    :historyTable="historyTableData"
                                />
                            </template>
                        </el-table-column>
                        <el-table-column prop="amount" label="金额(元)" class-name="historyData" align="right" width="120">
                            <template slot-scope="{ row }">
                                <el-input
                                    v-model="row.amount"
                                    placeholder="请输入内容"
                                    v-if="row.structureId && showParentUnit(row) ==='总额' && isEdit"
                                    @change="changeAmount(row)"
                                    v-enterAllInteger1
                                ></el-input>
                                <div v-else>{{ row.amount }}</div>
                                <historyUpdateTable
                                    v-if="checkEmpty(row.amountRecords)"
                                    @clickIcon="clickIcon(row.amountRecords)"
                                    :historyTable="historyTableData"
                                />
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
                <!-- 非生产性开支清单 -->
                <div class="e-tree-table" v-else-if="isBidding === 2 || (isBidding === 0 && contractType === 2)">
                    <el-table v-show="!isSearch" ref="tableRef"
                        border
                        style="width: 100%"
                        row-key="billId"
                        :data="tableData"
                        :tree-props="{
                            children: 'children',
                            hasChildren: 'hasChildren'
                        }"
                        :load="onload"
                        lazy
                        :row-class-name="tableRowClassName"
                        class="table"
                        :max-height="$store.state.tableHeight"
                        highlight-current-row
                        v-sortTable="{ 'tableData': tableData, '_this': this, 'ref': 'tableRef' }"
                    >
                        <el-table-column label="操作" width="200" align="left">
                            <template slot-scope="{ row }">
                                <div class="oparation_box">
                                    <img src="@/assets/btn/sub.png" class="add_sub"
                                        @click="addChildenProductive(row)"
                                        v-if="(row.listIsDetailed || row.identification === '项') && isAdd"
                                        title="添加子级"
                                        />
                                    <i class="el-icon-delete"
                                        style="font-size: 24px; color: #e9513e;"
                                        v-if="isAdd"
                                        @click="onDelNode(row)"
                                        title="删除"
                                    ></i>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="itemNo" label="细目编号" min-width="200" align="left">
                            <template slot-scope="{ row }">
                                <el-input
                                    v-model="row.itemNo"
                                    placeholder="请输入内容"
                                    v-if="isAdd"
                                ></el-input>
                                <div v-else>{{ row.itemNo }}</div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="itemName" label="细目名称" min-width="200" align="left">
                            <template slot-scope="{ row }">
                                <el-input
                                    v-model="row.itemName"
                                    placeholder="请输入内容"
                                    v-if="isAdd"
                                ></el-input>
                                <div v-else>{{ row.itemName }}</div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="quantity" label="数量" class-name="historyData"  align="right"  width="120">
                            <template slot-scope="{ row }">
                                <el-input
                                    v-model="row.quantity"
                                    placeholder="请输入内容"
                                    v-if="row.isDetailed && isEdit"
                                    v-enterFloat2
                                    @change="onCalcAmount(row,'quantityUpdateFlag')"
                                ></el-input>
                                <div v-else-if="!row.isDetailed"></div>
                                <div v-else> {{ row.quantity }} </div>
                                <historyUpdateTable
                                    v-if="checkEmpty(row.numberRecords)"
                                    @clickIcon="clickIcon(row.numberRecords)"
                                    :historyTable="historyTableData"
                                />
                            </template>
                        </el-table-column>
                        <el-table-column prop="unitPrice" label="单价" class-name="historyData" align="right" width="120">
                            <template slot-scope="{ row }">
                                <el-input
                                    v-model="row.unitPrice"
                                    placeholder="请输入内容"
                                    v-if="row.isDetailed && isEdit"
                                    v-enterFloat
                                    @change="onCalcAmount(row,'unitPriceUpdateFlag')"
                                ></el-input>
                                <div v-else-if="!row.isDetailed"></div>
                                <div v-else>{{ row.unitPrice }}</div>
                                <historyUpdateTable
                                    v-if="checkEmpty(row.priceRecords)"
                                    @clickIcon="clickIcon(row.priceRecords)"
                                    :historyTable="historyTableData"
                                />
                            </template>
                        </el-table-column>
                        <el-table-column prop="amount" label="金额(元)" class-name="historyData" align="right" width="120">
                        </el-table-column>
                    </el-table>

                    <el-table v-if="isSearch" ref="tableRef1"
                        border
                        style="width: 100%"
                        row-key="billId"
                        :data="searchData"
                        :tree-props="{
                            children: 'children',
                            hasChildren: 'hasChildren'
                        }"
                        :load="onload"
                        lazy
                        :row-class-name="tableRowClassName"
                        class="table"
                        :max-height="$store.state.tableHeight"
                        highlight-current-row
                        v-sortTable="{ 'tableData': searchData, '_this': this, 'ref': 'tableRef1' }"
                    >
                        <el-table-column  label="操作" width="200" align="left">
                        </el-table-column>
                        <el-table-column prop="itemNo" label="细目编号" min-width="200" align="left">
                            <template slot-scope="{ row }">
                                <el-input
                                    v-model="row.itemNo"
                                    placeholder="请输入内容"
                                    v-if="isAdd"
                                ></el-input>
                                <div v-else>{{ row.itemNo }}</div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="itemName" label="细目名称" min-width="200" align="left">
                            <template slot-scope="{ row }">
                                <el-input
                                    v-model="row.itemName"
                                    placeholder="请输入内容"
                                    v-if="isAdd"
                                ></el-input>
                                <div v-else>{{ row.itemName }}</div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="quantity" label="数量" class-name="historyData"  align="right"  width="120">
                            <template slot-scope="{ row }">
                                <el-input
                                    v-model="row.quantity"
                                    placeholder="请输入内容"
                                    v-if="row.isDetailed && isEdit"
                                    v-enterFloat2
                                    @change="onCalcAmount(row,'quantityUpdateFlag')"
                                ></el-input>
                                <div v-else-if="!row.isDetailed"></div>
                                <div v-else>{{ row.quantity }}</div>
                                <historyUpdateTable
                                    v-if="checkEmpty(row.numberRecords)"
                                    @clickIcon="clickIcon(row.numberRecords)"
                                    :historyTable="historyTableData"
                                />
                            </template>
                        </el-table-column>
                        <el-table-column prop="unitPrice" label="单价" class-name="historyData" align="right" width="120">
                            <template slot-scope="{ row }">
                                <el-input
                                    v-model="row.unitPrice"
                                    placeholder="请输入内容"
                                    v-if="row.isDetailed && isEdit"
                                    v-enterFloat
                                    @change="onCalcAmount(row,'unitPriceUpdateFlag')"
                                ></el-input>
                                <div v-else-if="!row.isDetailed"></div>
                                <div v-else>{{ row.unitPrice }}</div>
                                <historyUpdateTable
                                    v-if="checkEmpty(row.priceRecords)"
                                    @clickIcon="clickIcon(row.priceRecords)"
                                    :historyTable="historyTableData"
                                />
                            </template>
                        </el-table-column>
                        <el-table-column prop="amount" label="金额(元)" class-name="historyData" align="right" width="120">
                        </el-table-column>
                    </el-table>
                </div>
            </div>
            <div v-if="radio === 2">
                <div class="e-tree-table">
                    <el-table ref="tableRef2"
                        key="tableRef2"
                        border
                        style="width: 100%"
                        row-key="billId"
                        :data="tableData1"
                        :tree-props="{
                            children: 'children',
                            hasChildren: 'hasChildren'
                        }"
                        lazy
                        class="table"
                        :max-height="$store.state.tableHeight"
                        highlight-current-row
                        v-sortTable="{ 'tableData': tableData1, '_this': this, 'ref': 'tableRef2' }"
                    >
                        <el-table-column fixed  label="操作" width="200" align="left">
                        </el-table-column>
                        <el-table-column prop="structureName" fixed label="结构部位" width="200" align="left" show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column prop="itemName"  fixed label="细目名称" min-width="200" align="left" show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column prop="itemNo" fixed label="细目编号" min-width="200" align="left" show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column prop="unit" align="center" label="计量单位" width="120">
                        </el-table-column>
                        <el-table-column prop="identification"  align="center"  label="标识" width="120">
                            <template slot-scope="{ row }">
                                <div v-if="!row.structureId">{{ row.identification }}</div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="unitPriceType"  align="center"  label="单价类型" width="150">
                            <template slot-scope="{ row }">
                                    <div>{{ ['全单价','劳务单价'][row.unitPriceType-1] }}</div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="quantity" label="数量" class-name="historyData"  align="right"  width="120">
                        </el-table-column>
                        <el-table-column prop="unitPrice" label="单价" class-name="historyData" align="right" width="120">
                        </el-table-column>
                        <el-table-column prop="amount" label="金额(元)" class-name="historyData" align="right" width="120">
                        </el-table-column>
                    </el-table>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
// import tenderRequest from '@/api/tender/tender.js'
import { toLocation, vueDom } from '@/utils/common.js'
import { uidBuilder } from '@/api/base/uid.js'
import { getUnit } from '@/api/subcontractApi/currency.js'
import { isFourInput, isNumInput } from '@/utils/rules.js'
import { selectDataClient, showLoading, hideLoading, parseList, getItemOfTreeArr } from '@/utils/common'
import { mapMutations } from 'vuex'
import historyUpdateTable from '@/pages/subcontract/components/historyUpdateTable.vue'
import fn from '@/utils/checkEmpty.js'
export default {
    props: {
        billid: {
            type: String,
            default: ''
        },
        contractListTable: { //组件绑定的数据
            type: Array,
            default: function () {
                return []
            }
        },
        errorMsg: { //表格错误提示
            type: Array,
            default: function () {
                return []
            }
        },
        isBidding: { //清单引入方式 0：从招标引入 1：从目标预算引入 2：从非生产性开支引入
            type: Number,
            default: 1
        },
        mainInfo: { //主体信息
            type: Object,
            default: function () {
                return {}
            }
        },
        auditState: { //是否可以编辑
            type: Object,
            default: function () {
                return {
                    canAudit: false,
                    canModify: true,
                    canUndoAudit: false,
                    freeNextUnits: [],
                    isFree: false,
                }
            }
        },
        //是否可以新增
        isAdd: {
            default: true
        },
        //是否可以编辑
        isEdit: {
            default: true
        },
        subcontract: { //接口
        },
        state: { //流程状态
            type: Number
        },
        dtlList: { //包件清单
            type: Array,
            default: function () {
                return []
            }
        },
        contractType: {},
        isShowRadio: {
            default: false
        },
    },
    model: {
        prop: 'contractListTable',
        event: 'change'
    },
    computed: {
        tenderId () {
            return this.$route.query.billid
        },
        isUnitprice () { //计算最子级清单是否是劳务单价
            return row=>{
                const fn  = row=>{//查找到最子级清单
                    if(row.listIsDetailed && !row.structureId) {
                        return row
                    }else{
                        return fn(this.mapTableData[row.parentId])
                    }
                }
                let list = fn(row)
                if(list.unitPriceType === 2) {
                    return true
                }else{
                    return false
                }
            }
        },
        //判断数据是否为空
        checkEmpty () {
            return datas=>{
                return !fn.checkEmpty(datas)
            }
        },
    },
    components: {
        historyUpdateTable,
    },
    watch: {
        contractListTable: {
            handler (newName) {
                this.tableData = newName
            },
            immediate: false,
            deep: true
        },
        tableData: {
            handler (newName) {
                this.$emit('change', newName)
            },
            immediate: true,
            deep: true
        },
        dtlList: {
            async handler (val) {
                this.$emit('isMonitorTaxRate')
                const length = val.length
                const uids = await this.uidBuilder({ num: length })
                for(let i = 0;i < val.length;i++) {
                    val[i].uid = uids[i]
                }
                let arr = []
                if(this.contractType === 1) {
                    const fn = (data, parentId)=>{
                        let arr = val.filter(x=>x.parentId === parentId)
                        let arr1 = arr.map(x=>x.code)
                        arr1 = arr1.sort()
                        arr1.forEach(item=>{
                            data.push( arr.find(x=>x.code === item) )
                        })
                        if(data.length > 0) {
                            data.forEach(item=>{
                                if(item.isAllLeaf === 1) {
                                    item.children = []
                                }else{
                                    item.children = []
                                    fn(item.children, item.listId)
                                }

                            })
                        }
                    }
                    fn(arr, '0')
                    this.handleContractListBidding(arr)
                }else{
                    val = val.map(x=>{
                        let obj = {
                            'billId': x.billId,
                            'changeType': 0,
                            'costItem': x.codeName,
                            'dtlId': '',
                            'isLeaf': x.isLeaf,
                            'listId': x.listId,
                            'noTaxPrice': x.price,
                            'noTaxSubTotal': x.amount,
                            'parentId': x.parentId,
                            'qty': x.num,
                            'remark': '',
                            'resourceId': '',
                            'unproductiveNo': x.code,
                            'uid': x.uid
                        }
                        return obj
                    })
                    let arr = []
                    const fn = (data, parentId)=>{
                        let arr = val.filter(x=>x.parentId === parentId)
                        let arr1 = arr.map(x=>x.unproductiveNo)
                        arr1 = arr1.sort()
                        arr1.forEach(item=>{
                            data.push( arr.find(x=>x.unproductiveNo === item) )
                        })
                        if(data.length > 0) {
                            data.forEach(item=>{
                                if(item.isLeaf === 1) {
                                    item.children = []
                                }else{
                                    item.children = []
                                    fn(item.children, item.listId)
                                }

                            })
                        }
                    }
                    fn(arr, '0')
                    this.productiveExpensesList(arr)
                }
            },
            immediate: false,
            deep: true
        }
    },
    data () {
        return {
            detailsOfProposedArr: [],
            tableData: [],
            mapTableData: {},
            addList: [], // 缓存新增数据
            unitList: [],
            searchKey: '',
            isSearch: false,
            materialList: [],
            showTable: true,
            historyTableData: {},
            radio: 1,
            tableData1: []
        }
    },
    created () {
        this.getData()
    },
    mounted () {
        vueDom(
            // 默认0为选中项
            () => {
                $('.el-tree-node:first')?.addClass('is-current')
            },
            () => {
                return $('.el-tree-node:first')?.attr('class')?.includes('is-current')
            }
        )
        getUnit().then(res=>{
            let arr = res.map(x=>{
                let obj = {
                    value: '',
                    label: '',
                }
                obj.value = x.kvValue
                obj.label = x.kvKey
                return obj
            })
            this.unitList = arr
        })
        if(this.isShowRadio && !this.isAdd && !this.isEdit && this.contractType !== 2) {
            this.getById1()
        }
    },
    methods: {
        ...mapMutations(
            'contract', ['setMaterialList']
        ),
        ...mapMutations(['setHistoryTableData']),
        ...mapMutations(['setSelectedInfo']),
        // 获取数据
        getData () {
            this.showTable = false
            const params = {
                parentId: '0',
                sourceId: this.billid
            }
            this.subcontract.getContractList(params).then(res=>{
                res = res.code ? [] : res
                res.forEach(item=>{
                    item.hasChildren = item.isDetailed ? false : true
                    item.changeType = 0
                    item.children = []
                    if(this.state === 1) {
                        item.quantityUpdateFlag = false
                        item.unitPriceUpdateFlag = false
                        item.amountUpdateFlag = false
                    }
                })
                this.mapTableData['0'] = {
                    children: res
                }
                this.tableData = res
                this.showTable = true
            })
        },
        // 通过id获取子级数据
        async getById (billId) {
            const params = {
                parentId: billId,
                sourceId: this.billid
            }
            let result = await this.subcontract.getContractList(params)
            result.forEach(item=>{
                item.hasChildren = item.isDetailed ? false : true
                item.children = []
                item.changeType = 0
                item.listIsDetailed = item.listIsDetailed !== null ? item.listIsDetailed : false
                item.itemIsDetailed = item.itemIsDetailed !== null ? item.itemIsDetailed : false
                if(this.state === 1) {
                    item.quantityUpdateFlag = false
                    item.unitPriceUpdateFlag = false
                    item.amountUpdateFlag = false
                }
            })
            return result
        },
        // 通过id获取子级数据 结构-》细目
        async getById1 () {
            const params = {
                sourceId: this.billid
            }
            let result = await this.subcontract.getALLListByStructure(params)
            this.tableData1 = parseList(result, 'billId', 'parentId', '0')
            const data = this.tableData1
            result.filter(x=>x.isDetailed).forEach(row=>{
                // 在树中找出当前元素的位置 结构
                const stack = getItemOfTreeArr(data, 'billId', row.billId)
                // 从父级开始汇总，向上到顶级
                stack.pop() // 当前级出栈
                let parent = stack.pop()
                while (parent) {
                    parent.amount = parent.children.reduce((count, prev) => count + Number(prev.amount), 0)
                    parent = stack.pop()
                }
            })
        },
        importList () {
            if(this.isSearch) return
            this.showClient()
        },
        async showClient () {

            if (!this.mainInfo.secondParty.name) {
                this.clientPop('info', '请选择主体信息的乙方名称')
                return
            }
            if (this.isBidding === 0) {
                //招标清单
                selectDataClient({
                    SelectServiceToken: '/tender/engineeringOther/wpf/getList', // 客户端对应方法名
                    IsSingleMode: false, // 是否单选 true 单选 false 多选
                    SelectedIDs: [], // 已选中id
                    InitParamsStr: this.mainInfo.secondParty.dtlId,
                    Title: '选择清单' // 标题
                }).then(async val=>{
                    const length = val.length
                    const uids = await this.uidBuilder({ num: length })
                    for(let i = 0;i < val.length;i++) {
                        val[i].uid = uids[i]
                    }
                    let arr = []
                    if(this.contractType === 1) {
                        if(!val.find(x=>x.structureId)) {
                            this.clientPop('err', '请选择结构部位')
                            return false
                        }
                        const fn = (data, parentId)=>{
                            let arr = val.filter(x=>x.parentId === parentId)
                            let arr1 = arr.map(x=>x.code)
                            arr1 = arr1.sort()
                            arr1.forEach(item=>{
                                data.push( arr.find(x=>x.code === item) )
                            })
                            if(data.length > 0) {
                                data.forEach(item=>{
                                    if(item.isAllLeaf === 1) {
                                        item.children = []
                                    }else{
                                        item.children = []
                                        fn(item.children, item.listId)
                                    }

                                })
                            }
                        }
                        fn(arr, '0')
                        this.handleContractListBidding(arr)
                    }else{
                        val = val.map(x=>{
                            let obj = {
                                'billId': x.billId,
                                'changeType': 0,
                                'costItem': x.codeName,
                                'dtlId': '',
                                'isLeaf': x.isLeaf,
                                'listId': x.listId,
                                'noTaxPrice': x.price,
                                'noTaxSubTotal': x.amount,
                                'parentId': x.parentId,
                                'qty': x.num,
                                'remark': '',
                                'resourceId': '',
                                'unproductiveNo': x.code,
                                'uid': x.uid
                            }
                            return obj
                        })
                        let arr = []
                        const fn = (data, parentId)=>{
                            let arr = val.filter(x=>x.parentId === parentId)
                            let arr1 = arr.map(x=>x.unproductiveNo)
                            arr1 = arr1.sort()
                            arr1.forEach(item=>{
                                data.push( arr.find(x=>x.unproductiveNo === item) )
                            })
                            if(data.length > 0) {
                                data.forEach(item=>{
                                    if(item.isLeaf === 1) {
                                        item.children = []
                                    }else{
                                        item.children = []
                                        fn(item.children, item.listId)
                                    }

                                })
                            }
                        }
                        fn(arr, '0')
                        this.productiveExpensesList(arr)
                    }

                })
            } else if(this.isBidding === 1) {
                //目标预算清单
                let list = []
                showLoading()
                let listAll = await this.subcontract.contractListAll({ sourceId: this.billid })//获取所有数据
                this.setChildren([...listAll])
                let newListAll = []
                const treeChangeList = data=>{
                    data.forEach(item=>{
                        if(item.children.length > 0) {
                            treeChangeList(item.children)
                        }
                        newListAll.push(item)
                    })
                }
                treeChangeList(this.tableData)//将表格数据展平
                newListAll = newListAll.filter(x=>x.changeType === -1 && x.oriBillId)
                const changeChildType = data=>{
                    let obj = listAll.find(x=>x.billId === data.billId)
                    if(obj) {
                        obj.changeType = -1
                    }
                    if(!data.isDetailed) {
                        const arr = listAll.filter(x=>x.parentId === data.billId)
                        arr.forEach(item=>{
                            changeChildType(item)
                        })
                    }
                }
                newListAll.forEach(item=>{
                    changeChildType(item)
                })//将子级改成已删除状态
                listAll = listAll.filter(x=>x.changeType !== -1)//过滤已删除的源单并且删掉
                let structureIdArr = listAll.filter(x=>x.structureId)
                if(listAll.length > 0) {
                    listAll.forEach(item=>{
                        if(item.oriBillId && !item.structureId) {
                            let obj = {
                                'identification': item.identification,
                                'isDetailed': item.oriIsDetailed ? 1 : 0, //原清单的最明细
                                'itemName': item.itemName,
                                'itemNo': item.itemNo,
                                'quantity': item.quantity,
                                'unit': item.unit,
                                'thisLevelId': item.oriBillId,
                                'termIsDetailed': item.listIsDetailed ? 1 : 0,
                                'parentId': item.oriParentBillId,
                            }
                            let arr = structureIdArr.filter(x=>x.oriParentBillId === obj.thisLevelId)//找到当前级的结构部位集合
                            arr = arr.map(x=>{
                                let obj1 = {
                                    'billId': x.structureId,
                                    'structureName': x.structureName,
                                    'structureNamePaths': x.structureNamePaths,
                                    'structurePaths': x.structurePaths,
                                    'thisLevelId': x.oriBillId,
                                    'parentId': x.oriParentBillId,
                                    'quantity': x.quantity,
                                }
                                return obj1
                            })
                            obj.structure = arr
                            list.push(obj)
                        }

                    })
                }
                hideLoading()
                selectDataClient({
                    SelectServiceToken: 'EngineerSelectorImportDetail', // 客户端对应方法名
                    IsSingleMode: false, // 是否单选 true 单选 false 多选
                    SelectedIDs: [], // 已选中id
                    InitParamsStr: JSON.stringify(list),
                    Title: '选择清单' // 标题
                }).then(async val => {
                    const valFilter = val.filter(x=>x.structure.length > 0)
                    const newVal = []
                    val.forEach(item=>{
                        newVal.push(item)
                    })
                    valFilter.forEach(item=>{
                        item.structure.forEach(x=>{
                            newVal.push(x)
                        })
                    })
                    const length = newVal.length
                    const uids = await this.uidBuilder({ num: length })
                    for(let i = 0;i < newVal.length;i++) {
                        newVal[i].uid = uids[i]
                    }
                    let arr = []
                    const fn = (data, parentId)=>{
                        let arr = val.filter(x=>x.parentId === parentId)
                        let arr1 = arr.map(x=>x.itemNo)
                        arr1 = arr1.sort()
                        arr1.forEach(item=>{
                            data.push( arr.find(x=>x.itemNo === item) )
                        })
                        if(data.length > 0) {
                            data.forEach(item=>{
                                if(item.isLeaf === false) {
                                    item.children = []
                                }else{
                                    item.children = []
                                    fn(item.children, item.thisLevelId)
                                }

                            })
                        }
                    }
                    fn(arr, '0')
                    this.handleContractList(arr)
                })
            }else if(this.isBidding === 2) {
                //非生产性开支清单
                selectDataClient({
                    SelectServiceToken: '/project/up/getDtlByOrgId', // 客户端对应方法名
                    IsSingleMode: false, // 是否单选 true 单选 false 多选
                    SelectedIDs: [], // 已选中id
                    InitParamsStr: '',
                    Title: '选择清单' // 标题
                }).then(async val => {
                    const length = val.length
                    const uids = await this.uidBuilder({ num: length })
                    for(let i = 0;i < val.length;i++) {
                        val[i].uid = uids[i]
                    }
                    let arr = []
                    const fn = (data, parentId)=>{
                        let arr = val.filter(x=>x.parentId === parentId)
                        let arr1 = arr.map(x=>x.unproductiveNo)
                        arr1 = arr1.sort()
                        arr1.forEach(item=>{
                            data.push( arr.find(x=>x.unproductiveNo === item) )
                        })
                        if(data.length > 0) {
                            data.forEach(item=>{
                                if(item.isLeaf === 1) {
                                    item.children = []
                                }else{
                                    item.children = []
                                    fn(item.children, item.listId)
                                }

                            })
                        }
                    }
                    fn(arr, '0')
                    this.productiveExpensesList(arr)
                })

            }
        },
        //引入目标预算清单
        async handleContractList  (arr) {
            const fn =  (newData, oldData, parentId)=>{
                newData.forEach(async x=>{
                    const resul1 = oldData.find(item=>item.oriBillId === x.thisLevelId && item.changeType !== -1)
                    if(!resul1) {
                        let obj = {
                            'amount': '',
                            'billId': '',
                            'changeType': 1,
                            'children': [],
                            'itemNo': x.itemNo,
                            'identification': x.identification,
                            'itemName': x.itemName,
                            'quantity': 0,
                            'sourceId': this.billid,
                            'structuralPosition': '',
                            'unit': x.unit,
                            'unitPrice': 0,
                            'unitPriceType': '',
                            'structureId': '',
                            'structureName': '',
                            'structureNamePaths': '',
                            'structurePaths': '',
                            'isDetailed': false, //是否最明细
                            'oriBillId': x.thisLevelId,
                            'oriParentBillId': x.parentId,
                            'itemIsDetailed': x.isDetailed, //细目最明细
                            'listIsDetailed': x.termIsDetailed//清单最明细
                        }
                        obj.parentId = parentId
                        obj.billId = x.uid //本级ID
                        obj.oriIsDetailed = x.isDetailed//原清单是否最明细
                        obj.hasChildren = true
                        obj.isNew = true
                        this.mapTableData[obj.billId] = obj
                        if(!x.isDetailed ) {
                            fn(x.children, obj.children, obj.billId)
                        }else{
                            const structure = x.structure
                            for(let item of structure) {
                                let obj1 = {
                                    'amount': '',
                                    'billId': item.uid,
                                    'changeType': 1,
                                    'children': [],
                                    'itemNo': '',
                                    'identification': x.identification,
                                    'itemName': '',
                                    'quantity': item.quantity ? item.quantity : 0,
                                    'sourceId': this.billid,
                                    'structuralPosition': '',
                                    'unit': x.unit,
                                    'unitPrice': 0,
                                    'unitPriceType': '',
                                    'structureId': item.billId,
                                    'structureName': item.structureName,
                                    'structureNamePaths': item.structureNamePaths,
                                    'structurePaths': item.structurePaths,
                                    'isDetailed': true, //是否最明细
                                    'oriBillId': x.thisLevelId,
                                    'oriParentBillId': x.parentId,
                                    'itemIsDetailed': false, //细目最明细
                                    'listIsDetailed': false, //清单最明细
                                    'oriIsDetailed': null,
                                    'useSupplyFlag': false, //是否参与甲供清单的计算
                                    hasChildren: false,
                                }
                                obj1.parentId = obj.billId
                                obj1.isNew = true
                                this.mapTableData[obj1.billId] = obj1
                                obj.children.push(obj1)
                            }
                            // 统计所有同级
                            const countSibling = obj.children.reduce((prev, item) => {
                                return prev + Math.round(item.changeType !== -1 ? item.quantity : 0)
                            }, 0)
                            // 赋值汇总数据给父级
                            obj.quantity = countSibling // 赋值汇总数据给父级
                        }
                        oldData.push(obj)
                    }else{
                        if(!x.isDetailed ) {
                            if(resul1.children.length === 0) {
                                resul1.billId = resul1.thisLevelId ? resul1.thisLevelId : resul1.billId
                                const res = await this.getById(resul1.billId, resul1)
                                res.forEach(item=>{
                                    resul1.children.push(item)
                                })
                            }
                            fn(x.children, resul1.children, resul1.billId)
                        }
                    }

                })
            }

            fn(arr, this.tableData, '0')
        },
        //引入招标清单
        async handleContractListBidding  (arr) {
            const fn =  (newData, oldData, parentId)=>{
                newData.forEach(async x=>{
                    const resul1 = oldData.find(item=>item.oriBillId === x.listId && item.changeType !== -1)
                    if(!resul1) {
                        let obj
                        if(x.structureId) {
                            obj = {
                                'amount': 0,
                                'billId': '',
                                'changeType': 1,
                                'children': [],
                                'itemNo': '',
                                'identification': x.label,
                                'itemName': '',
                                'quantity': x.num  ? x.num : 0,
                                'sourceId': this.billid,
                                'structuralPosition': '',
                                'unit': this.mapTableData[parentId].unit,
                                'unitPrice': x.pricex ? x.pricex : 0,
                                'unitPriceType': x.priceType,
                                'structureId': x.structureId,
                                'structureName': x.structureName,
                                'structureNamePaths': x.structureNamePaths,
                                'structurePaths': x.structurePaths,
                                'isDetailed': true, //是否最明细
                                'oriBillId': x.listId,
                                'oriParentBillId': x.parentId,
                                'itemIsDetailed': false, //细目最明细
                                'listIsDetailed': false, //清单最明细
                                'useSupplyFlag': false, //是否参与甲供清单的计算
                                'oriPrice': x.price ? x.price : 0, //原单单价
                                hasChildren: false,

                            }
                            if(obj.unit === '总额') {
                                obj.amount = x.amount
                            }
                            this.mapTableData[parentId].itemIsDetailed = true
                        }else{
                            obj = {
                                'amount': x.amount,
                                'changeType': 1,
                                'children': [],
                                'itemNo': x.code,
                                'identification': x.label,
                                'itemName': x.codeName,
                                'quantity': x.num ? x.num : 0,
                                'sourceId': this.billid,
                                'structuralPosition': '',
                                'unit': x.unit,
                                'unitPrice': x.pricex  ? x.pricex : 0,
                                'unitPriceType': x.priceType,
                                'structureId': '',
                                'structureName': '',
                                'structureNamePaths': '',
                                'structurePaths': '',
                                'isDetailed': false, //是否最明细
                                'oriBillId': x.listId,
                                'oriParentBillId': x.parentId,
                                'itemIsDetailed': x.isLeaf, //细目最明细
                                'listIsDetailed': x.isListLeaf, //清单最明细
                                'oriPrice': x.price ? x.price : 0, //原单单价
                                hasChildren: true,
                            }
                        }
                        obj.parentId = parentId
                        obj.billId = x.uid //本级ID
                        obj.oriIsDetailed = x.isListLeaf//原清单是否最明细
                        obj.isNew = true
                        this.mapTableData[obj.billId] = obj
                        oldData.push(obj)
                        if( x.isAllLeaf === 0 ) {
                            fn(x.children, obj.children, obj.billId)
                        }else{
                            this.onCalcAmount(obj)
                        }
                    }else{
                        if( x.isAllLeaf === 0 ) {
                            if(resul1.children.length === 0) {
                                resul1.billId = resul1.thisLevelId ? resul1.thisLevelId : resul1.billId
                                const res = await this.getById(resul1.billId, resul1)
                                res.forEach(item=>{
                                    resul1.children.push(item)
                                })
                            }
                            fn(x.children, resul1.children, resul1.billId)
                        }
                    }

                })
            }

            fn(arr, this.tableData, '0')
            hideLoading()
        },
        //设置树的数据
        setChildren (newData) {
            const fn = data=>{
                //找到同级的新数据
                if(!data.isDetailed) {
                    const newArr = newData.filter(x=>x.parentId === data.billId)
                    data.children =  data.children ? data.children : []
                    newArr.forEach(item=>{
                        const obj = this.mapTableData[item.billId]
                        if(!obj) {
                            item.hasChildren = item.isDetailed ? false : true
                            item.changeType = 0
                            item.children = []
                            if(this.state === 1) {
                                item.quantityUpdateFlag = false
                                item.unitPriceUpdateFlag = false
                                item.amountUpdateFlag = false
                            }
                            data.children.push(item)
                        }
                    })
                    data.children.forEach(item=>{
                        fn(item)
                    })
                    this.mapTableData[data.billId] = data
                }
            }
            this.tableData.forEach(item=>{
                fn(item)
            })
        },
        //引入非生产性开支清单
        async productiveExpensesList  (arr) {
            const fn =  (newData, oldData, parentId)=>{
                newData.forEach(async x=>{
                    const resul1 = oldData.find(item=>item.oriBillId === x.listId && item.changeType !== -1)
                    if(!resul1) {
                        let obj = {
                            'amount': 0,
                            'billId': '',
                            'changeType': 1,
                            'children': [],
                            'itemNo': x.unproductiveNo,
                            'identification': '清单',
                            'itemName': x.costItem,
                            'quantity': x.qty ? x.qty : 0,
                            'sourceId': this.billid,
                            'structuralPosition': '',
                            'unit': '',
                            'unitPrice': x.noTaxPrice ? x.noTaxPrice : 0,
                            'unitPriceType': '',
                            'structureId': '',
                            'structureName': '',
                            'isDetailed': x.isLeaf === 0 ? false : true, //是否最明细
                            'oriBillId': x.listId,
                            'oriParentBillId': x.parentId,
                            'itemIsDetailed': !x.isLeaf, //细目最明细
                            'listIsDetailed': !x.termIsDetailed//清单最明细
                        }
                        obj.parentId = parentId
                        obj.billId = x.uid //本级ID
                        obj.oriIsDetailed = x.isLeaf === 0 ? false : true//原清单是否最明细
                        obj.hasChildren = x.isLeaf === 0 ? true : false
                        obj.isNew = true
                        this.mapTableData[obj.billId] = obj
                        oldData.push(obj)
                        if( x.isLeaf === 0 ) {
                            fn(x.children, obj.children, obj.billId)
                        }else{
                            this.onCalcAmount(obj)
                        }
                    }else{
                        if( x.isLeaf === 0 ) {
                            if(resul1.children.length === 0) {
                                const res = await this.getById(resul1.billId, resul1)
                                res.forEach(item=>{
                                    resul1.children.push(item)
                                })
                            }
                            fn(x.children, resul1.children, resul1.billId)
                        }
                    }

                })
            }

            fn(arr, this.tableData, '0')
        },
        async onload (row, treeNode, resolve) {
            let result
            if(row.children.length > 0) {
                result = row.children
            }else{
                // 如果不是最子级，就从接口取
                if (!row.isDetailed) {
                    row.billId = row.thisLevelId ? row.thisLevelId : row.billId
                    result = await this.getById(row.billId, row)
                } else {
                // 是最子级，则初始为空，合并新增数据，并设为非最子级
                    result = []
                    row.isDetailed = false
                }
                // 如果有缓存数据，说明是点击的 “添加子级” 按钮
                if (this.addList.length > 0) {
                    result = result.concat(this.addList)
                    this.addList = []
                }
            }
            row.treeNode = treeNode
            row.resolve = resolve
            row.children = result
            this.mapTableData[row.billId] = row
            if(this.isBidding !== 2 && this.contractType !== 2) {
                result.forEach(item=>{
                    if(row.unit !== '总额' && row.itemIsDetailed) {
                        if(item.unitPrice !== row.unitPrice) {
                            item.unitPrice = row.unitPrice
                            item.oriPrice = row.oriPrice
                            this.onCalcAmount(item)
                        }
                    }
                    this.mapTableData[item.billId] = item
                })
            }
            resolve(result)
        },
        // 添加子级
        async addChilden (row) {
            const { billId } = row
            const uid = await this.uidBuilder({ num: 1 })
            let addData = {
                'amountUpdateFlag': false,
                'changeState': 0,
                'itemIsDetailed': true,
                'oriParentBillId': '',
                'quantityUpdateFlag': false,
                'remarks': '',
                'structureNamePaths': '',
                'structurePaths': '',
                'unitPriceUpdateFlag': false,
                'amount': '',
                'changeType': 1,
                'children': [],
                'itemNo': '',
                'identification': '项',
                'itemName': '',
                'quantity': 0,
                'sourceId': this.billid,
                'structuralPosition': '',
                'unit': '',
                'unitPrice': 0,
                'unitPriceType': row.unitPriceType,
                'structureId': '',
                'structureName': '',
                'oriIsDetailed': null,
                'oriBillId': '',
                listIsDetailed: false,
                parentId: billId, //父级ID
                billId: uid, //本级ID
                isDetailed: true,
                hasChildren: false,
                isNew: true,
            }
            row.itemIsDetailed = false
            addData.itemIsDetailed = true//是否是明细最子级
            /*
                如果是清单最子级新增，下面没有项则新增一项，结构部位下移到项，如果有项，则新增一项，将其他最子级项的结构部位复制到新增项里面
                如果是项新增子级，则在改项下新增一级项，把结构部位下移到新增项下面
            */
            const getChildren = ()=>{
                return new Promise(resolve=>{
                    let children = []
                    const fn  = async data=>{
                        if(data[0].isDetailed) {
                            const index = data.length
                            data.forEach(async (x, i)=>{
                                if(x.changeType !== -1) {
                                    let obj = {
                                        'amount': '',
                                        'changeType': x.changeType,
                                        'children': [],
                                        'itemNo': '',
                                        'identification': '项',
                                        'itemName': '',
                                        'quantity': 0,
                                        'sourceId': this.billid,
                                        'structuralPosition': '',
                                        'unitPrice': 0,
                                        'unitPriceType': '',
                                        'structureId': x.structureId,
                                        'structureName': x.structureName,
                                        'oriIsDetailed': null,
                                        'oriBillId': x.oriBillId,
                                        listIsDetailed: false,
                                        parentId: '', //父级ID
                                        billId: x.billId, //本级ID
                                        isDetailed: true,
                                        hasChildren: false,
                                        isNew: true,
                                        //后续新增的字段
                                        'amountUpdateFlag': false,
                                        'changeState': 0,
                                        'itemIsDetailed': null,
                                        'oriParentBillId': x.oriParentBillId,
                                        'quantityUpdateFlag': false,
                                        'remarks': '',
                                        'structureNamePaths': x.structureNamePaths,
                                        'structurePaths': x.structurePaths,
                                        'unit': '',
                                        'unitPriceUpdateFlag': false
                                    }
                                    addData.isDetailed = false
                                    addData.hasChildren = true
                                    children.push(obj)
                                }
                                if(i === index - 1) {
                                    resolve(children)
                                }
                            })
                        }else{
                            if(data[0].children.length > 0) {
                                fn(data[0].children)
                            }else{
                                const params = {
                                    parentId: data[0].billId,
                                    sourceId: this.billid
                                }
                                let result = await this.subcontract.getContractList(params)
                                fn(result)
                            }
                        }
                    }
                    if(!row.isDetailed) { //如果不是最明细就往下找
                        if(row.children.length > 0) {
                            fn(row.children)
                        }else{
                            const params = {
                                parentId: row.billId,
                                sourceId: this.billid
                            }
                            this.subcontract.getContractList(params).then(result=>{
                                fn(result)
                            })
                        }
                    }
                })
            }
            getChildren().then(children=>{
                new Promise(resolve=>{
                    if(row.children[0].isDetailed) {
                        row.children.splice(0)
                        children.forEach(item=>{
                            item.parentId = addData.billId
                            if(item.changeType === 0) {
                                item.changeType = 2
                            }
                        })
                        resolve(children)
                    }else{
                        this.uidBuilder({ num: children.length }).then(uids=>{
                            uids = children.length > 1 ? uids : [uids]
                            children.forEach((item, index)=>{
                                item.parentId = addData.billId
                                item.billId = uids[index]
                                item.changeType = 1
                            })
                            resolve(children)
                        })
                    }
                }).then(children=>{
                    addData.children = children ? children : []
                    this.mapTableData[addData.billId] = addData
                    const tableRef = this.$refs.tableRef
                    if (!this.mapTableData[billId]) {
                        // 缓存新增数据
                        this.addList.push(addData)
                        // 如果是最子级，则设为目录
                        if (!row.hasChildren) {
                            row.children = row.children ? row.children : []
                            row.children = row.children.length > 0 ? row.children.push(addData) : [addData]
                            row.hasChildren = true
                            row.isDetailed = false
                            this.mapTableData[billId] = row
                            this.onCalcAmount(addData)
                            // 切换展开状态
                            this.$nextTick(() => {
                                // tableRef.toggleRowExpansion(row, true)
                                tableRef.store.loadOrToggle(row)
                            })
                        // }
                        } else {
                            // 展开子级
                            // tableRef.store.loadData(row, billId)
                            tableRef.store.loadOrToggle(row)
                        }
                    } else {
                        // 已经展开，则直接添加
                        row.children.push(addData)
                        row.hasChildren = true
                        row.isDetailed = false
                    }
                    // 定位到新添加行
                    toLocation(addData)
                })

            })
        },
        // 添加非生产性开支清单子级
        async addChildenProductive (row) {
            const { billId } = row
            const uid = await this.uidBuilder({ num: 1 })
            let addData = {
                'amount': '',
                'changeType': 1,
                'children': [],
                'itemNo': '',
                'identification': '项',
                'itemName': '',
                'quantity': 0,
                'sourceId': this.billid,
                'structuralPosition': '',
                'unit': '',
                'unitPrice': 0,
                'unitPriceType': row.unitPriceType,
                'structureId': '',
                'structureName': '',
                'oriIsDetailed': null,
                'oriBillId': '',
                listIsDetailed: false,
                parentId: billId, //父级ID
                billId: uid, //本级ID
                isDetailed: true,
                hasChildren: false,
                isNew: true
            }
            addData.itemIsDetailed = true//是否是明细最子级
            this.mapTableData[addData.billId] = addData
            const tableRef = this.$refs.tableRef
            if (!this.mapTableData[billId]) {
                // 缓存新增数据
                this.addList.push(addData)
                // 如果是最子级，则设为目录
                if (!row.hasChildren) {
                    row.children = row.children ? row.children : []
                    row.children = row.children.length > 0 ? row.children.push(addData) : [addData]
                    row.hasChildren = true
                    row.isDetailed = false
                    this.mapTableData[billId] = row
                    this.onCalcAmount(addData)
                    // 切换展开状态
                    this.$nextTick(() => {
                        // tableRef.toggleRowExpansion(row, true)
                        tableRef.store.loadOrToggle(row)
                    })
                    // }
                } else {
                    // 展开子级
                    // tableRef.store.loadData(row, billId)
                    tableRef.store.loadOrToggle(row)
                }
            } else {
                // 已经展开，则直接添加
                row.children.push(addData)
                row.hasChildren = true
                row.isDetailed = false
            }
            // 定位到新添加行
            toLocation(addData)
        },
        onDelNode (row) {
            const deleteFn = row=>{
                const deleteStru = row=>{//删除结构部位
                    const siblings = this.mapTableData[row.parentId].children
                    const index = siblings.findIndex(x=>x.billId === row.billId)
                    if(siblings[index].changeType === 1) {
                        siblings.splice(index, 1)
                    }else{
                        siblings[index].changeType = -1
                    }
                    this.onCalcAmount(row)
                }

                const fn1 = data=>{ //向下删除
                    let newListAll = []
                    const treeChangeList = data=>{
                        data.forEach(item=>{
                            if(item.children.length > 0) {
                                treeChangeList(item.children)
                            }
                            newListAll.push(item)
                        })
                    }
                    if(data.structureId) {
                        deleteStru(data)
                    }else{
                        if(data.children.length > 0) {
                            treeChangeList(data.children)//将表格数据展平
                        }
                        for(let i = 0;i < newListAll.length;i++) {
                            if(newListAll[i].structureId) {
                                deleteStru(newListAll[i])
                            }else{
                                if(newListAll[i].changeType === 1) {
                                    newListAll.splice(i, 1)
                                    i--
                                }else{
                                    newListAll[i].changeType = -1
                                }
                                this.onCalcAmount(newListAll[i])
                            }
                        }
                    }
                    // if(data.children.length > 0) {
                    //     for(let i = 0;i < data.children.length;i++)
                    //     {
                    //         fn1(data.children[i])
                    //         if(data.children.length > 0) {
                    //             if(data.children[i].changeType === 1) {
                    //                 data.children.splice(i, 1)
                    //                 i--
                    //             }else{
                    //                 data.children[i].changeType = -1
                    //             }
                    //             this.onCalcAmount(data.children[i])
                    //         }
                    //     }
                    // }else{
                    //     deleteStru(data)
                    // }
                }
                fn1(row)
                const fn = data=>{ //向上删除
                    if(data.parentId !== '0') {
                        const siblings = this.mapTableData[data.parentId].children
                        const index = siblings.findIndex(x=>x.billId === data.billId)
                        const filterSibings = siblings.filter(x=>x.changeType !== -1)
                        if(filterSibings.length > 1) {
                            if(siblings[index].changeType === 1) {
                                siblings.splice(index, 1)
                            }else{
                                siblings[index].changeType = -1
                            }
                            this.onCalcAmount(siblings[index])
                        }else{
                            fn(this.mapTableData[data.parentId])
                        }
                    }else{
                        const index = this.tableData.findIndex(x=>x.billId === data.billId)
                        if(this.tableData[index].changeType === 1) {
                            this.tableData.splice(index, 1)
                        }
                        else{
                            this.tableData[index].changeType = -1
                        }
                        fn1(data)
                        this.mapTableData['0'].children = this.tableData
                    }
                }
                fn(row)
            }
            // 如果是新增的，则直接删除
            if (row.changeType === 1) {
                deleteFn(row)
            } else {
                // 否则先提示，再删除
                this.clientPop('err', this.oneDeleteListErr, () => {
                    deleteFn(row)
                })
            }
        },
        // 输入后计划金额
        onCalcAmount (row, feild) {
            this.$emit('isMonitorTaxRate')
            if(this.state === 1 && feild) {
                row[feild] = true
            }
            if(this.isBidding === 2 || (this.isBidding === 0 && this.contractType === 2)) {
                row.amount = row.changeType !== -1 ? Math.round(( parseFloat(row.quantity) ? parseFloat(row.quantity) : 0 ) * ( parseFloat(row.unitPrice) ? parseFloat(row.unitPrice) : 0 ))  : 0

            }else{
                if(!row.structureId) return
                if(row.structureId && this.showParentUnit(row) === '总额') {
                    console.log('')
                }else{
                    row.amount = row.changeType !== -1 ? Math.round(( parseFloat(row.quantity) ? parseFloat(row.quantity) : 0 ) * ( parseFloat(row.unitPrice) ? parseFloat(row.unitPrice) : 0 ))  : 0
                }
            }
            if(row.changeType !== -1) {
                row.changeType = row.changeType === 1   ? 1 : 2
            }
            // 向上汇总金额
            this.recurCount({
                parentId: row.parentId,
                rootId: '0',
            })
        },
        //递归统计父级
        // recurCount ({ parentId, rootId, listField, parentField, row }) {
        //     // 到根节点
        //     if (parentId === rootId) {
        //         return
        //     }
        //     const parent = this.mapTableData[parentId]
        //     // 统计所有同级
        //     let countSibling = parent.children.reduce((prev, item) => {
        //         item.amount = item.amount ? parseInt(item.amount) : item.amount
        //         return prev + (item.changeType !== -1 ? item.amount : 0)
        //     }, 0)
        //     // 赋值汇总数据给父级
        //     countSibling = typeof (countSibling) === 'string' ? parseInt(countSibling) : countSibling
        //     parent.amount = parseInt(countSibling.toFixed(2))
        //     if(parent.changeType !== -1) {
        //         parent.changeType = parent.changeType === 1 ? 1 : 2
        //     }
        //     // 未到根节点则继续遍历父节点
        //     return this.recurCount({
        //         parentId: parent[parentField],
        //         rootId,
        //         listField,
        //         parentField,
        //         row,
        //     })
        // },
        // 循环统计父级
        recurCount ({ parentId }) {
            let treeId = parentId
            while(treeId || treeId !== '0') {
                const parent = this.mapTableData[treeId]
                if(!parent) return
                // 统计所有同级
                let countSibling = parent.children.reduce((prev, item) => {
                    item.amount = item.amount ? parseInt(item.amount) : item.amount
                    return prev + (item.changeType !== -1 ? item.amount : 0)
                }, 0)
                // 赋值汇总数据给父级
                countSibling = typeof (countSibling) === 'string' ? parseInt(countSibling) : countSibling
                parent.amount = parseInt(countSibling)
                if(parent.changeType !== -1) {
                    parent.changeType = parent.changeType === 1 ? 1 : 2
                }
                if(parent.parentId === treeId) return
                treeId = parent.parentId
            }

        },
        tableRowClassName ({ row }) {
            if(row.changeType === -1) {
                if(row.hasChildren) {
                    return `class-${row.billId} row-hide`
                }else{
                    return `class-${row.billId}  row-hide hide-icon`
                }
            }else{
                if(row.hasChildren) {
                    return `class-${row.billId} row-show`
                }else{
                    return `class-${row.billId}  row-show hide-icon`
                }
            }
        },

        async uidBuilder (params) {
            let temp = ''
            await uidBuilder(params).then(res => {
                temp = res.length === 1 ? res[0] : res
            })
            return temp
        },
        // refreshLoadTree (parentId) {
        //     // 根据父级id取出对应节点数据
        //     // const { row, treeNode, resolve } = this.mapTableData[parentId]
        //     let children = this.mapTableData[parentId].children
        //     const tableRef = this.$refs.tableRef
        //     this.$set(tableRef.store.states.lazyTreeNodeMap, parentId, children)
        // },

        isFourInput (event) {
            isFourInput(event)
        },
        isNumInput (event) {
            isNumInput(event)
        },
        search () {
            this.isSearch = false
            this.searchData = []
            this.$emit('isSearch', this.isSearch)
            if(this.searchKey.length > 0) {
                this.searchData = []
                this.subcontract.searchContractList({
                    key: this.searchKey,
                    sourceId: this.billid
                }).then(res=>{
                    this.isSearch = true
                    //平级列表转成树形结构
                    let arr = []
                    const fn = (data, parentId)=>{
                        let arr = res.filter(x=>x.parentId === parentId)
                        let arr1 = arr.map(x=>x.itemNo)
                        arr1 = arr1.sort()
                        arr1.forEach(item=>{
                            data.push( arr.find(x=>x.itemNo === item) )
                        })
                        if(data.length > 0) {
                            data.forEach(item=>{
                                item.hasChildren = !item.isDetailed
                                item.changeType = 0
                                if(item.isDetailed) {
                                    item.children = []
                                }else{
                                    item.children = []
                                    fn(item.children, item.billId)
                                }

                            })
                        }
                    }
                    fn(arr, '0')
                    this.searchData = arr
                    this.$emit('isSearch', this.isSearch, this.searchData, this.searchKey)
                })
            }
        },
        //单价类型切换
        unitPriceTypeChange (row) {
            const fn = async data=>{
                if(!data.isDetailed) {
                    if(data.children.length > 0) {
                        data.children.forEach(item=>{
                            item.unitPriceType = data.unitPriceType
                            this.mapTableData[data.billId] = data
                            fn(item)
                        })
                    }else{
                        const params = {
                            parentId: data.billId,
                            sourceId: this.billid
                        }
                        let result = await this.subcontract.getContractList(params)
                        data.children = result.map(x=>{
                            x.changeType = 0
                            x.hasChildren = !x.isDetailed
                            x.children = []
                            return x
                        })
                        fn(data)
                    }
                }else{
                    this.editChangeType(data)
                }

            }
            if(!row.isDetailed) {
                fn(row)
            }else{
                this.mapTableData[row.billId] = row
            }
        },
        //改变当前分支的changeType
        editChangeType (row) {
            const fn = row=>{
                row.changeType = row.changeType === 1 ? 1 : 2
                if(row.parentId !== '0') {
                    const parent = this.mapTableData[row.parentId]
                    fn(parent)
                }
            }
            fn(row)
        },
        //单价切换
        async unitPriceChange (row) {
            if(this.state === 1) {
                row.unitPriceUpdateFlag = true
            }
            if(row.children.length > 0) {
                row.children.forEach(item=>{
                    if(row.unit !== '总额' && row.itemIsDetailed) {
                        if(item.unitPrice !== row.unitPrice) {
                            item.unitPrice = row.unitPrice
                            this.onCalcAmount(item)
                        }
                    }
                })
                this.mapTableData[row.billId] = row
            }else{
                row.billId = row.thisLevelId ? row.thisLevelId : row.billId
                const result = await this.getById(row.billId, row)
                result.forEach(item=>{
                    item.hasChildren = item.isDetailed ? false : true
                    item.children = []
                    item.changeType = 0
                    if(this.state === 1) {
                        item.quantityUpdateFlag = false
                        item.unitPriceUpdateFlag = false
                        item.amountUpdateFlag = false
                    }
                    if(row.unit !== '总额' && row.itemIsDetailed) {
                        if(item.unitPrice !== row.unitPrice) {
                            item.unitPrice = row.unitPrice
                            this.onCalcAmount(item)
                        }
                    }
                    this.mapTableData[item.billId] = item
                    row.children.push(item)
                    this.onCalcAmount(item)
                })
            }
        },
        //数量切换
        quantityChange (row) {
            if(this.state === 1) {
                row.quantityUpdateFlag = true
            }
            const parent = this.mapTableData[row.parentId]
            // 统计所有同级
            const countSibling = parent.children.reduce((prev, item) => {
                let quantity = parseInt(item.changeType !== -1 ? item.quantity : 0)
                quantity = quantity ? quantity : 0
                return prev + quantity
            }, 0)
            // 赋值汇总数据给父级
            parent.quantity = countSibling // 赋值汇总数据给父级
            this.onCalcAmount(row)
        },
        //计量单位切换
        changeUnit (row) {
            if(row.unit === '总额') {
                row.quantity = 0
                row.unitPrice = 0
                row.children.forEach(item=>{
                    item.quantity = 0
                    item.unitPrice = 0
                    item.amount = 0
                    this.onCalcAmount(item)
                })
            }

        },
        //金额切换
        changeAmount (row) {
            if(this.state === 1) {
                row.amountUpdateFlag = true
            }
            this.onCalcAmount(row)
        },
        clearData () {
            const arr = [ ...this.tableData]
            const deleteFn = row=>{
                const deleteStru = row=>{//删除结构部位
                    const siblings = this.mapTableData[row.parentId].children
                    const index = siblings.findIndex(x=>x.billId === row.billId)
                    if(siblings[index].changeType === 1) {
                        siblings.splice(index, 1)
                    }else{
                        siblings[index].changeType = -1
                        this.onCalcAmount(row)
                    }
                }

                const fn1 = data=>{ //向下删除
                    let newListAll = []
                    const treeChangeList = data=>{
                        data.forEach(item=>{
                            if(item.children.length > 0) {
                                treeChangeList(item.children)
                            }
                            newListAll.push(item)
                        })
                    }
                    if(data.children.length > 0) {
                        treeChangeList(data.children)//将表格数据展平
                    }
                    for(let i = 0;i < newListAll.length;i++) {
                        if(newListAll[i].structureId) {
                            deleteStru(newListAll[i])
                        }else{
                            if(newListAll[i].changeType === 1) {
                                newListAll.splice(i, 1)
                                i--
                            }else{
                                newListAll[i].changeType = -1
                                this.onCalcAmount(newListAll[i])
                            }
                        }
                    }
                }
                fn1(row)
                const fn = data=>{ //向上删除
                    if(data.parentId !== '0') {
                        const siblings = this.mapTableData[data.parentId].children
                        const index = siblings.findIndex(x=>x.billId === data.billId)
                        const filterSibings = siblings.filter(x=>x.changeType !== -1)
                        if(filterSibings.length > 1) {
                            if(siblings[index].changeType === 1) {
                                siblings.splice(index, 1)
                            }else{
                                siblings[index].changeType = -1
                            }
                            this.onCalcAmount(siblings[index])
                        }else{
                            fn(this.mapTableData[data.parentId])
                        }
                    }else{
                        const index = this.tableData.findIndex(x=>x.billId === data.billId)
                        if(this.tableData[index].changeType === 1) {
                            this.tableData.splice(index, 1)
                        }
                        else{
                            this.tableData[index].changeType = -1
                        }
                        fn1(data)
                        this.mapTableData['0'].children = this.tableData
                    }
                }
                fn(row)
            }
            arr.forEach(item=>{
                deleteFn(item)
            })
        },
        clickIcon (list) {
            this.setHistoryTableData(list)
        },
        showParentUnit (row) {
            try{
                const parent = this.mapTableData[row.billId].unit
                return parent
            }catch(err) {
                return ''
            }
        }
    }
}
</script>
<style lang='scss' scoped>
    ::v-deep .hide-icon{
        .el-table__expand-icon{
            display: none;
        }
    }
    .right{
        .el-input {
            width:350px
        }
    }
    ::v-deep.el-table__row{
        .cell{
            display: flex;
            align-items: center;
        }
        td:nth-child(5),td:nth-child(6),td:nth-child(7){
            .cell{
                display: block;
            }
        }
        td:nth-child(8),td:nth-child(9),td:nth-child(10){
            .cell{
                justify-content: flex-end;
            }
        }
    }
    ::v-deep .el-popover__reference-wrapper{
        display: flex;
        height: 100%;
    }
</style>
